package com.linesum.code.domain.entity;

import java.util.Date;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * baseEntity基础类拦截器
 * @author chenzf
 *
 */
@SuppressWarnings("static-method")
public class BaseEntityListener {
	/**The logger. */
	private static Logger logger = LoggerFactory.getLogger(BaseEntityListener.class);

	@PrePersist
	public void prePresist(Object object) {
		if (object instanceof BaseEntity) {
			BaseEntity baseEntity = (BaseEntity) object;
			setLastModifyTime(baseEntity);
		}
	}

	private void setLastModifyTime(BaseEntity baseEntity) {
		if (baseEntity.getLastModifyTime() == null) {
			baseEntity.setLastModifyTime(new Date());
		}
	}

	@PreUpdate
	public void preUpdate(Object object) {
		if (object instanceof BaseEntity) {
			BaseEntity baseEntity = (BaseEntity) object;
			setLastModifyTime(baseEntity);
			logger.info("{} 对象 (ID : {}) 被 {} 在 {} 修改",
					new Object[] { object.getClass().getName(), baseEntity.getId(), null, new Date() });
		}
	}

}
